|
|
|
|
|
|
|
val property. The Interval property can be set to any value between zero and 65,535. Setting the Interval property to zero disables the Timer control. Otherwise, the Interval property specifies the amount of time in milliseconds between firings of the Timer event. In other words, if you want 10 seconds to elapse, set the value to 10,000. |
|
|
|
|
|
|
|
|
|
If the Enabled property is set to True at designtime, the timer begins countdown as soon as the form containing it is loaded.  |
|
|
|
|
|
|
|
|
|
|
Because the maximum value of the Interval corresponds to about a minute, how can you set up longer time intervals? You set up code within the Timer event that uses the system time to determine whether your interval has elapsed, as shown in Listing 4.2. The code in this listing causes the current time to be printed in the Immediate window every five minutes. |
|
|
|
|
|
|
|
|
Listing 4.2 TIMEREX.ZIPUsing a Timer Control for Long Intervals |
|
|
|
|
|
|
|
|
Option Explicit
Dim dtNextTime As Date
Private Sub Form_Load()
'Set up the timer
Timer1.Interval = 500
Timer1.Enabled = True
'We want to do something every 5 minutes
dtNextTime = DateAdd("n", 5, Now)
End Sub
Private Sub Timer1_Timer()
If Now >= dtNextTime Then
Timer1.Enabled = False
'Do what you want here then update dtNextTime
Debug.Print "Timer Event at " & Now
dtNextTime = DateAdd("n", 5, Now)
Timer1.Enabled = True
End If
End Sub |
|
|
|
|
|
|
|
|
The code in Listing 4.2 does not use the Timer control to execute an event; instead, it uses the Timer control to check the system date to see whether it needs to execute an event. The key to this listing is the DateAdd function, which is used to compute a date five minutes in the future. |
|
|
|
|
|
|
|
|
|
A code-only alternative to using the Timer control is the Timer function, which returns the number of seconds elapsed since midnight. You can subtract the current return value of this function from a stored value to find the number of elapsed seconds, as in the following example: |
|
|
|
|
|
|
|
|
Dim sngStart As Single
sngStart = Timer
' (perform some action)
Debug.Print "Elasped time was " & CInt(Timer sngStart) & " seconds."  |
|
|
|
|
|
|
|